package com.cn.wanxi.servlet.back.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.DataEncoding;
import com.cn.wanxi.util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/back/product/info")
public class ProductInfoServlet extends HttpServlet {
    IProductService productService = new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setTextContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //产品详情
        String pid = req.getParameter("id");
        int id = 0;
        if (!"".equals(pid))
            id = Integer.parseInt(pid);
        ProductModel productModel = productService.getProduct(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("product",productModel);
        resp.getWriter().print(jsonObject);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //产品模糊查询
        String name = req.getParameter("name");
        String minPrice = req.getParameter("min_price");
        String maxPrice = req.getParameter("max_price");
        String product_type = req.getParameter("product_type");
        String user_type = req.getParameter("user_type");
        String pageSize = req.getParameter("page");
        String pageNum = req.getParameter("size");
        int page = Integer.parseInt(pageSize);
        int size = Integer.parseInt(pageNum);
        //SQL语句拼接
        String sql = "select * from product where 1=1 ";
        if (!name.equals(""))
            sql+="and name='"+name+"'";
        if (!product_type.equals("全部"))
            sql+="and product_type='"+product_type+"'";
        if (!user_type.equals("全部"))
            sql+="and user_type='"+user_type+"'";
        if (!minPrice.equals("")) {
            BigDecimal min_price = new BigDecimal(minPrice);
            sql += "and price>='" + min_price + "'";
        }
        if (!maxPrice.equals("")) {
            BigDecimal max_price = new BigDecimal(maxPrice);
            sql += "and price<='" + max_price + "'";
        }
        long total = JDBCUtils.getCount(sql.replace("*","count(*)"));
        sql+=" limit "+ (page-1)*size +","+size;
        List<ProductModel> productList = productService.searchProduct(sql);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("products",productList);
        jsonObject.put("total",total);
        resp.getWriter().print(jsonObject);
    }
}
